package com.example.jpetstoreclient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jpetstoreclient.common.CommonResponse;
import com.example.jpetstoreclient.domain.Animal;
import com.example.jpetstoreclient.domain.History;
import com.example.jpetstoreclient.persistance.HistoryMapper;
import com.example.jpetstoreclient.service.AnimalService;
import com.example.jpetstoreclient.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("historyService")
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {

    @Autowired
    AnimalService animalService;

    @Override
    public CommonResponse<History> addHistory(History history) {
        if (save(history))return CommonResponse.createForSuccess("保存历史成功",history);
        else return CommonResponse.createForError("保存历史失败");
    }

    @Override
    public CommonResponse<List<History>> getHistoryByUserId(int user_id) {
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user_id);
        wrapper.orderByDesc("time");

        List<History> histories = list(wrapper);
        for(History history : histories){
            int animal_id = history.getAnimal_id();
            Animal animal = animalService.getById(animal_id);
            history.setAnimal(animal);
        }
        return CommonResponse.createForSuccess("查询用户历史记录",histories);
    }
}














